window.addEventListener('DOMContentLoaded',main);
var selectStudent = new Set();
var totalStudent = 0;
var deleteButton;
var newButton;
function main() {
    var checkBoxs = document.querySelectorAll('.aho-table tr[data-id] td:nth-child(1)').forEach((ele)=>{
        ele.addEventListener('click',(e)=>{
            e.stopPropagation();
        });
    });

    var trs = document.querySelectorAll('.aho-table tr[data-id]').forEach((ele)=>{
        ele.addEventListener('click',(e)=>{
            let id = e.currentTarget.getAttribute('data-id');
            let url = urlPrefix+detailView+id;
            window.location = url;
        });
    });
    newButton = document.querySelector('#new');
    if (newButton != null) {
	    newButton.addEventListener('click',(e)=>{
	        if (e.currentTarget.classList.contains('bt-disable')) {
	            console.log("new was disabled");
	            return;
	        }
	        let url = urlPrefix+'/new';
	        if (withId)
	        	url += '?id=' + id;
	        window.location = url;
	        console.log("new button on click");
	    });
	}
    deleteButton = document.querySelector('#delete');
    if (deleteButton != null)
	    deleteButton.addEventListener('click',(e)=>{
	        if (e.currentTarget.classList.contains('bt-disable')) {
	            console.log("delete was disabled");
	            return;
	        }
	        console.log("delete button on click");
	        document.querySelector('#deleteForm').submit();
	    });
    var checkboxs = document.querySelectorAll('.checkbox');
    totalStudent = checkboxs.length;
    checkboxs.forEach((ele)=>{
        ele.addEventListener('change',(e)=>{
            if (e.currentTarget.checked) {
                selectStudent.add(e.currentTarget);
                if (selectStudent.size == totalStudent) {
                    document.querySelector('#toggleAll').checked = true;
                }
                deleteButton.classList.remove('bt-disable');
            } else {
                selectStudent.delete(e.currentTarget);
                let toggleAll = document.querySelector('#toggleAll');
                if (toggleAll.checked) {
                    toggleAll.checked = false;
                }
                if (selectStudent.size == 0) {
                    deleteButton.classList.add('bt-disable');
                }
            }
        });
    });
    document.querySelector('#toggleAll').addEventListener('click',(e)=>{
        if (e.currentTarget.checked) {
            checkboxs.forEach((ele)=>{selectStudent.add(ele)});
            deleteButton.classList.remove('bt-disable');
        } else {
            selectStudent.clear();
            deleteButton.classList.add('bt-disable');
        }
        checkboxs.forEach((ele)=>{
            ele.checked = e.currentTarget.checked;
        });
    });
    var searchForm = document.querySelector("#search-form");
    var order = document.querySelector("#order");
    var dir = document.querySelector("#dir");
    var ths = document.querySelectorAll("table.aho-table > tbody > tr:nth-child(1) > th[data-id]");
    ths.forEach((e) => {
		if (e.getAttribute("data-id") === order.value) {
			if (dir.value === "1") {
				e.querySelector("div").classList.toggle("tri-down");
			} else {
				e.querySelector("div").classList.toggle("tri-up");
			}
		}
		e.addEventListener("click", (_) => { 
			
			let same = order.value === e.getAttribute("data-id");
			order.value = e.getAttribute("data-id");
			if (same && dir.value === "1") {
				dir.value = "0";
			} else if (same && dir.value === "0") {
				dir.value = "1";
			} else {
				dir.value = 1;
			}
			
			searchForm.submit();
		});
	});
}